#
# rbenchmarks: An R package to contain benchmarks for the language shootout
# author: Shane Conway <shane.conway@gmail.com>
#
# This is released under a GPL license.
#
# Documentation was created using roxygen:
# roxygenize('rbenchmarks', roxygen.dir='rbenchmarks', copy.package=FALSE, unlink.target=FALSE)
#
###############################################################################

#
# Mandelbrot Set
# Site: http://shootout.alioth.debian.org/u32/performance.php?test=mandelbrot
# Definition: http://mathworld.wolfram.com/MandelbrotSet.html
#

# Each program should plot the Mandelbrot set [-1.5-i,0.5+i] on an N-by-N bitmap. Write output byte-by-byte in portable bitmap format.

#
# References:
# 	1. http://blog.revolutionanalytics.com/2010/09/mandelbrot-set.html
#	2. http://www.marcoscan.com/2009/12/frattali-con-r.html
#	2. http://rosettacode.org/wiki/Mandelbrot_set

# From Rosetta Code (distributed under GNU FDL: http://en.wikipedia.org/wiki/GNU_Free_Documentation_License)

createmandel <- function(zfrom, zto, inc=0.01, maxiter=100) {
  x <- c()
  y <- c()
  cost <- zfrom
  while( Re(cost) <= Re(zto) ) {
    cost <- Re(cost) + Im(zfrom)*1i
    while ( Im(cost) <= Im(zto) ) {
      j <- 0
      z <- 0
      while( (abs(z) < 2) && (j < maxiter)) {
        z <- z^2 + cost
        j <- j + 1
      }
      if ( j == maxiter ) {
        x <- c(x, Re(cost))
        y <- c(y, Im(cost))
      }
      cost <- cost + inc*1i
    }
    cost <- cost + inc
  }
  plot(x,y, pch=".")
}
 
createmandel(-2-1i, 1+1i)